package com.billy.ohos.swipe.demo;

import com.billy.ohos.swipe.SmartSwipe;
import com.billy.ohos.swipe.SmartSwipeBack;
import com.billy.ohos.swipe.SmartSwipeRefresh;
import com.billy.ohos.swipe.ext.refresh.ArrowDrawable;
import com.billy.ohos.swipe.ext.refresh.ArrowHeader;
import com.billy.ohos.swipe.refresh.ClassicFooter;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilityPackage;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

public class MyApplication extends AbilityPackage {

    @Override
    public void onInitialize() {
        super.onInitialize();


        //add global swipe back for all abilities
        // for more details: https://qibilly.com/SmartSwipe-tutorial/pages/SmartSwipeBack.html

//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//            //use bezier back before LOLLIPOP
//            SmartSwipeBack.abilityBezierBack(this, abilitySwipeBackFilter);
//        } else {
            //add relative moving slide back
            SmartSwipeBack.abilitySlidingBack(this, abilitySwipeBackFilter);
////        }
//
//        // demo: set the default creator of refresh view with SmartSwipeRefresh for global usage
        SmartSwipeRefresh.setDefaultRefreshViewCreator(new SmartSwipeRefresh.SmartSwipeRefreshViewCreator() {
            @Override
            public SmartSwipeRefresh.SmartSwipeRefreshHeader createRefreshHeader(Context context) {
                ArrowHeader arrowHeader = new ArrowHeader(context);
                int height = SmartSwipe.dp2px(100, context);
                ComponentContainer.LayoutConfig layoutParams =  new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, height);
                arrowHeader.setLayoutConfig(layoutParams);
                arrowHeader.setInitializer(new ArrowHeader.IArrowInitializer() {
                    @Override
                    public void onArrowInit(ArrowHeader arrowHeader, ArrowDrawable arrowDrawable) {
                        arrowDrawable.setBowColor(Color.GRAY.getValue());
                        arrowDrawable.setArrowColor(Color.BLACK.getValue());
                        arrowDrawable.setStringColor(Color.GRAY.getValue());
                        arrowDrawable.setLineColor(Color.GRAY.getValue());

                        ShapeElement element = new ShapeElement();
                        element.setRgbColor(RgbColor.fromArgbInt(Color.LTGRAY.getValue()));
                        arrowHeader.setBackground(element);
                    }
                });
                return arrowHeader;
            }

            @Override
            public SmartSwipeRefresh.SmartSwipeRefreshFooter createRefreshFooter(Context context) {
                return new ClassicFooter(context);
            }
        });
    }

    private SmartSwipeBack.AbilitySwipeBackFilter abilitySwipeBackFilter = new SmartSwipeBack.AbilitySwipeBackFilter() {
        @Override
        public boolean onFilter(Ability ability) {
            return !(ability instanceof MainAbility);
        }
    };
}
